package com.jmb.apiplatform.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jmb.apiplatform.common.PageResult;
import com.jmb.apiplatform.exception.BusinessException;
import com.jmb.apiplatform.mapper.ApiCaseSuiteCaseRefMapper;
import com.jmb.apiplatform.pojo.ApiCaseSuiteCaseRef;
import com.jmb.apiplatform.pojo.ApiCaseSuiteCaseRefTO;
import com.jmb.apiplatform.pojo.ApiCaseSuiteCaseRefVO;
import com.jmb.apiplatform.service.ApiCaseSuiteCaseRefService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ApiCaseSuiteCaseRefServiceImpl implements ApiCaseSuiteCaseRefService {
    @Autowired
    private ApiCaseSuiteCaseRefMapper apiCaseSuiteCaseRefMapper;


    /**
     * 测试套件新增用例
     * @param caseSuiteCaseRef 用例集合
     */
    @Override
    public void saveSuiteCaseRef(List<ApiCaseSuiteCaseRef> caseSuiteCaseRef) {
        ArrayList<ApiCaseSuiteCaseRef> list = new ArrayList<>();
        for (ApiCaseSuiteCaseRef apiCaseSuiteCaseRef : caseSuiteCaseRef){
            Integer caseId = apiCaseSuiteCaseRef.getCaseId();
            Integer suiteId = apiCaseSuiteCaseRef.getSuiteId();
            ApiCaseSuiteCaseRefTO apiCaseSuiteCaseRefTO = new ApiCaseSuiteCaseRefTO();
            apiCaseSuiteCaseRefTO.setCaseId(caseId);
            apiCaseSuiteCaseRefTO.setSuiteId(suiteId);
            List<ApiCaseSuiteCaseRefVO> caseRefVOList = apiCaseSuiteCaseRefMapper.selectSuiteCaseList(apiCaseSuiteCaseRefTO);
            if (caseRefVOList.isEmpty()){
                list.add(apiCaseSuiteCaseRef);
            }
        }
        if (!list.isEmpty()){
            apiCaseSuiteCaseRefMapper.insertSuiteRef(list);
        }
    }

    /**
     * 插入单条记录
     * @param caseSuiteCaseRef 用例对象
     */
    @Override
    public void saveSuiteCaseRefSingle(ApiCaseSuiteCaseRef caseSuiteCaseRef) {
        apiCaseSuiteCaseRefMapper.insertSuiteCaseSingleRef(caseSuiteCaseRef);
    }

    /**
     * 修改测试套件的用例
     * @param caseSuiteCaseRef 用例对象
     */
    @Override
    public void editSuiteCaseRef(ApiCaseSuiteCaseRef caseSuiteCaseRef) {
        apiCaseSuiteCaseRefMapper.updateApiSuiteCaseRef(caseSuiteCaseRef);

    }

    /**
     * 删除测试套件内的用例
     * @param incrementKey 用例id
     */
    @Override
    public void removeSuiteCaseRef(Integer incrementKey) {
        apiCaseSuiteCaseRefMapper.deleteApiSuiteCaseRefById(incrementKey);

    }

    /**
     * 批量删除测试套件内的用例
     * @param caseSuiteCaseRef 用例对象
     */
    @Override
    public void removeSuiteCaseRefByObject(ApiCaseSuiteCaseRef caseSuiteCaseRef) {
        apiCaseSuiteCaseRefMapper.deleteApiSuiteCaseRefBySuiteId(caseSuiteCaseRef);

    }

    /**
     * 查询测试套件内的用例列表
     * @param caseSuiteCaseRefTO 用例对象
     * @return 分页的数据
     */
    @Override
    public PageResult<ApiCaseSuiteCaseRefVO> findSuiteCaseRefList(ApiCaseSuiteCaseRefTO caseSuiteCaseRefTO) {
        PageHelper.startPage(caseSuiteCaseRefTO.getPageNum(), caseSuiteCaseRefTO.getPageSize());
        List<ApiCaseSuiteCaseRefVO> list = apiCaseSuiteCaseRefMapper.selectSuiteCaseList(caseSuiteCaseRefTO);
        Page<ApiCaseSuiteCaseRefVO> page = (Page<ApiCaseSuiteCaseRefVO>) list;
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 查询测试套件内的用例列表
     * @param caseSuiteCaseRefTO 用例对象
     * @return 不分页的数据
     */
    @Override
    public List<ApiCaseSuiteCaseRefVO> findAllSuiteCaseRef(ApiCaseSuiteCaseRefTO caseSuiteCaseRefTO) {
        return apiCaseSuiteCaseRefMapper.selectSuiteCaseList(caseSuiteCaseRefTO);
    }

    @Override
    public String executeSuiteCaseById(Integer suiteId, String executor) throws BusinessException {
        return "";
    }

    @Override
    public Integer executeCaseInSuite(Integer suiteId, Integer caseId, String executor) throws BusinessException {
        return 0;
    }
}
